import jieba
import random
import jieba.analyse
from websites.crmebs.tpy.s_detail import getDetailHtml

fileName = r'/image.png'
detailName = r'/detail.png'

"""
商品属性
"""
goodsAttrArr = {"名称": "", "内配": "", "单位": "", "净含量": "", "零售价": "",
                "卖价": "", "包装尺寸": "", "保质期": "", "大图": ""}


def standard_1(bean, active, goodsType, baseImgPath):
    """
    规范1
    """

    if active == 0:  # 首行为项目名称，存储下来
        getStandardArr(bean)
        return

    name = ""
    config = ""
    weight = ""
    retailPrice = ""
    myShopPrice = ""
    size = ""
    ShelfLife = ""
    imgPath = ""
    unit = ""
    index = 0

    for item in bean:
        value = item.value

        if goodsAttrArr["名称"] == index:
            name = value
        elif goodsAttrArr["内配"] == index:
            config = value
        elif goodsAttrArr["净含量"] == index:
            weight = value
        elif goodsAttrArr["零售价"] == index:
            retailPrice = int(value)
        elif goodsAttrArr["卖价"] == index:
            myShopPrice = int(value)
        elif goodsAttrArr["包装尺寸"] == index:
            size = value
        elif goodsAttrArr["保质期"] == index:
            ShelfLife = value
        elif goodsAttrArr["单位"] == index:
            unit = value
        elif goodsAttrArr["大图"] == index:
            imgPath = value
        index = index + 1
    return getShopGoodsSql(name, config, unit, weight, retailPrice, myShopPrice, size, ShelfLife, imgPath, active,
                           goodsType, baseImgPath)


def getShopGoodsSql(name, config, unit, weight, retailPrice, myShopPrice, size, ShelfLife, imgPath, active, goodsType,
                    baseImgPath):
    """
    获取插入商品的SQL
    'sales': str(getSaleCount()),
              'stock': '999',
              'add_time': str(current_time()), 'spec_type': '1', 'activity': '0,1,2,3'
    """

    picArr = []
    attrsArr = []
    imgUrl = ""
    sqlMap = defaultSql(active, goodsType)

    if name != "":
        sqlMap["store_name"] = name
        sqlMap["store_info"] = name
        sqlMap["keyword"] = ','.join(jieba.analyse.extract_tags(name, round(1, 3), allowPOS=['n']))

    if imgPath != '':
        imgUrl = baseImgPath + str(int(imgPath)) + fileName
        picArr.append(imgUrl)
        sqlMap["image"] = imgUrl
        sqlMap["slider_image"] = picArr

    child = {}
    sqlMap["items"] = []
    attrsChild = {"pic": sqlMap["image"], "vip_price": 0, "stock": 999, "bar_code": "", "weight": 0,
                  "volume": 0, "brokerage": 0, "brokerage_two": 0}
    if weight != "":
        sqlMap["items"].append({"value": "商品规格", "detail": [weight]})
        child["商品规格"] = weight
        attrsChild["value2"] = weight

    if unit != "":
        sqlMap["items"].append({"value": "单位", "detail": [unit]})
        child["单位"] = unit
        attrsChild["value1"] = unit

    if weight == "" and unit == "":
        sqlMap["items"].append({"value": "规格", "detail": ["套餐一"]})
        child["规格"] = "套餐一"
        attrsChild["value1"] = "套餐一"

    attrsChild["detail"] = child
    attrsArr.append(attrsChild)

    if retailPrice != '' and attrsArr[0] is not None:  # 标价
        attrsArr[0]["ot_price"] = int(retailPrice)

    if myShopPrice != '' and attrsArr[0] is not None:  # 规格价格
        attrsArr[0]["cost"] = int(myShopPrice)
        attrsArr[0]["price"] = int(myShopPrice)  # 售价
        sqlMap["attrs"] = attrsArr

    images = getDetailHtml(config, size, ShelfLife, imgUrl)
    sqlMap["description"] = images
    return sqlMap


def defaultSql(active, goodsType):
    """
    获取固定的sql
    """
    map = {"disk_info": "", "sales": getSaleCount(), "logistics": ["1"], "freight": "4", "postage": 0,
           "recommend": [], "presale_day": 1,
           "presale": False, "video_open": False, "vip_product": False, "custom_form": [], "unit_name": "件",
           "is_show": 1, "is_benefit": 0, "is_postage": 0, "is_sub": [], "label_id": [], "selectRule": "",
           "coupon_ids": [], "command_word": "", "type": 0, "is_copy": 1, "couponName": [], "spec_type": 1,
           "is_virtual": 0, "video_link": "", "temp_id": 1, "virtual_type": 0, "recommend_list": [],
           "give_integral": 0, "recommend_image": "", "ficti": 0, "sort": 0, "id": 0,
           "activity": ["默认", "秒杀", "砍价", "拼团"]}

    if active == 1:
        map["is_hot"] = '1'
    elif active == 2:
        map["is_best"] = '1'
    elif active == 3:
        map["is_new"] = '1'
    elif active == 4:
        map["is_good"] = '1'

    map["cate_id"] = [goodsType]
    return map


def getStandardArr(bean):
    """
    标准化处理列内容
    """
    for item in bean:
        attrValue = goodsAttrArr.get(str(item.value))
        if attrValue is not None:
            goodsAttrArr[str(item.value)] = bean.index(item)


def getSaleCount():
    """
    随机生成销量
    """
    return random.randint(100, 300)
